package com.kai.steering.data.service.service.impl;

import com.kai.steering.common.web.service.impl.BaseServiceImpl;
import com.kai.steering.data.service.entity.domainObject.PermissionDO;
import com.kai.steering.data.service.entity.domainObject.RoleDO;
import com.kai.steering.data.service.entity.po.Permission;
import com.kai.steering.data.service.entity.po.Role;
import com.kai.steering.data.service.mapper.PermissionMapper;
import com.kai.steering.data.service.mapper.RoleMapper;
import com.kai.steering.data.service.service.PermissionService;
import com.kai.steering.data.service.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionService permissionService;


    @Override
    public RoleDO getRoleById(String id, boolean existencePermission) {
        Role roleById = roleMapper.getRoleById(id);
        RoleDO roleDO = new RoleDO(roleById);
        if (existencePermission) {
            List<PermissionDO> permissonByRole = permissionService.getPermissonByRole(id);
            roleDO.setPermissions(permissonByRole);
        }


            return roleDO;
    }

    @Override
    public List<RoleDO> getRoleByUser(String userId, boolean existencePermission) {
        List<Role> role = roleMapper.getRoleByUser(userId);
        List<RoleDO> collect = role.stream().map(permission -> {
            return new RoleDO(permission);
        }).collect(Collectors.toList());
        if (existencePermission&&collect.size()>0){
            for (RoleDO roleDO : collect) {
                List<PermissionDO> permissonByRole = permissionService.getPermissonByRole(roleDO.getId());
                List<PermissionDO> permissonByRole1 = permissonByRole;
                roleDO.setPermissions(permissonByRole1);
            }
        }
        return collect;
    }
}
